﻿using System;
using System.Net.Http.Headers;

namespace HW0310_5
{
    enum FigureKind
    {
        Rectangle = 0, Square = 1, Triangle = 2 // should be ","
    }
    public abstract class Figure
    {
        public abstract double GetArea();
        public abstract bool Evaluate();
    }
    class Rectangle : Figure
    {
        public double ledge;
        public double sedge;
        public double lEdge
        {
            get
            {
                return ledge; // still needs to do this
            }
            set
            {
                ledge = value;
            }
        }
        public double sEdge
        {
            get
            {
                return sedge;
            }
            set
            {
                sedge = value;
            }
        }
        public Rectangle()
        {
            Random r = new Random();
            lEdge = r.Next(50);
            sEdge = r.Next(50);
        }
        public override bool Evaluate()
        {
            if (lEdge >= 0 && sEdge >= 0)
                return true;
            else
                return false;
        }
        public override double GetArea()
        {
            if (Evaluate() == false)
                return 0;
            return lEdge * sEdge;
        }
    }
    class Square : Figure
    {
        public double edge;
        public double Edge
        {
            get
            {
                return edge;
            }
            set
            {
                edge = value;
            }
        }
        public Square()
        {
            Random r = new Random();
            Edge = r.Next(50);
        }
        public override bool Evaluate()
        {
            if (Edge >= 0)
                return true;
            else
                return false;
        }
        public override double GetArea()
        {
            if (Evaluate() == false)
                return 0;
            return Edge * Edge;
        }
    }
    class Triangle : Figure
    {
        public double edge1, edge2, edge3;
        public double Edge1
        {
            get
            {
                return edge1;
            }
            set
            {
                edge1 = value;
            }
        }
        public double Edge2
        {
            get
            {
                return edge2;
            }
            set
            {
                edge2 = value;
            }
        }
        public double Edge3
        {
            get
            {
                return edge3;
            }
            set
            {
                edge3 = value;
            }
        }
        public Triangle()
        {
            Random r = new Random();
            Edge1 = r.Next(50);
            Edge2 = r.Next(50);
            Edge3 = r.Next(50);
        }
        public override bool Evaluate()
        {
            if ((Edge1 >= 0 && Edge2 >= 0 && Edge3 >= 0) && (Edge1 + Edge2 > Edge3) && (Edge2 + Edge3 > Edge1) && (Edge1 + Edge3 > Edge2))
                return true;
            else
                return false;
        }
        public override double GetArea()
        {
            if (Evaluate() == false)
                return 0;
            double p = (Edge1 + Edge2 + Edge3) / 2;
            return Math.Sqrt(p * (p - Edge1) * (p - Edge2) * (p - Edge3));
        }
    }
    class FigureFactory
    {
        public static Figure GetFigure(FigureKind kind)
        {
            Figure figure = null;
            if (kind.Equals(FigureKind.Rectangle))
                figure = new Rectangle();
            else if (kind.Equals(FigureKind.Square))
                figure = new Square();
            else if (kind.Equals(FigureKind.Triangle))
                figure = new Triangle();
            return figure;
        }
    }
    
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Randomly generate 10 figure objects...");
            Random r = new Random();
            Figure[] figure = new Figure[10];
            for (int i = 0; i < 10; i++)
            {
                int r_num = r.Next(0, 3); // doesn't include 3
                figure[i] = FigureFactory.GetFigure((FigureKind)r_num); // just like this is okay
                Console.Write("{0,-20}", "    " + i + ": " + (FigureKind)r_num);
                Console.WriteLine("    Is legal? : " + figure[i].Evaluate());
            }
            Console.Write("Sum of all the areas of these figures is: ");
            double sum = 0;
            for (int i = 0; i<10; i++)
            {
                sum += figure[i].GetArea();
            }
            Console.WriteLine("{0:f4}", sum);
        }
    }
}